unit config;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ShellAPI, sqlite3, definition;

type
  TConfigForm = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Abzeichenart: TCheckBox;
    Pruefungsanzahl: TCheckBox;
    Label3: TLabel;
    Strasse: TCheckBox;
    Hausnummer: TCheckBox;
    Postleitzahl: TCheckBox;
    Ort: TCheckBox;
    Telefon: TCheckBox;
    eMail: TCheckBox;
    Uebernehmen: TButton;
    Abbrechen: TButton;
    StaticText1: TStaticText;
    latexpath: TEdit;
    Button1: TButton;
    Label4: TLabel;
    Label5: TLabel;
    StaticText2: TStaticText;
    Label6: TLabel;
    edtSportverein: TEdit;
    procedure AbbrechenClick(Sender: TObject);
    procedure UebernehmenClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Label5Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  end;

implementation

{$R *.dfm}

procedure TConfigForm.AbbrechenClick(Sender: TObject);
begin
  Close;
end;

procedure TConfigForm.UebernehmenClick(Sender: TObject);
  function BoolToIntChar(const B: Boolean): Char;
  begin
    if B then Result := '1' else Result := '0';
  end;
var
  db: TSqliteDatabase;
begin

  // Write configuration to database

  db := TSqliteDatabase.Open(DBFile);
  try
    db.ExecSQL('update configuration set '
      + 'check_abzeichenart=' + BoolToIntChar(Abzeichenart.Checked) + ', '
      + 'check_pruefungsanzahl=' + BoolToIntChar(Pruefungsanzahl.Checked) + ', '
      + 'check_strasse=' + BoolToIntChar(Strasse.Checked) + ', '
      + 'check_plz=' + BoolToIntChar(Postleitzahl.Checked) + ', '
      + 'check_hausnummer=' + BoolToIntChar(Hausnummer.Checked) + ', '
      + 'check_ort=' + BoolToIntChar(Ort.Checked) + ', '
      + 'check_telefon=' + BoolToIntChar(Telefon.Checked) + ', '
      + 'check_mail=' + BoolToIntChar(eMail.Checked) + ', '
      + 'latexpath="' + latexpath.Text + '", '
      + 'sportverein="' + edtSportverein.Text + '"');
  finally   
    db.Free;
  end;
  Close;

end;

procedure TConfigForm.Button1Click(Sender: TObject);
var
  open: TOpenDialog;
begin
  open := TOpenDialog.Create(Self);
  open.InitialDir := ExtractFilePath(latexpath.Text);
  open.DefaultExt := 'exe';
  open.Filter := 'PdfLaTeX (pdflatex.exe)|pdflatex.exe';
  try
    if open.Execute then
    begin
      latexpath.Text := open.FileName;
    end;
  finally
    open.Free;
  end;
end;

procedure TConfigForm.FormShow(Sender: TObject);
var
  db: TSqliteDatabase;
begin

  // Get configuration from database

  db := TSqliteDatabase.Open(DBFile);
  
  with db.Query('SELECT check_abzeichenart, check_pruefungsanzahl, '
    + 'check_strasse, check_plz, check_hausnummer, check_ort, check_telefon, '
    + 'check_mail, latexpath, sportverein FROM configuration limit 1') do
  begin
    if not Eof then
    begin
      Abzeichenart.Checked    := FieldAsInteger(0) = 1;
      Pruefungsanzahl.Checked := FieldAsInteger(1) = 1;
      Strasse.Checked         := FieldAsInteger(2) = 1;
      Postleitzahl.Checked    := FieldAsInteger(3) = 1;
      Hausnummer.Checked      := FieldAsInteger(4) = 1;
      Ort.Checked             := FieldAsInteger(5) = 1;
      Telefon.Checked         := FieldAsInteger(6) = 1;
      eMail.Checked           := FieldAsInteger(7) = 1;
      latexpath.Text          := FieldAsString(8);
      edtSportverein.Text     := FieldAsString(9);
    end;
    Free;
  end;
  
  db.Free;

end;

procedure TConfigForm.Label5Click(Sender: TObject);
begin
  ShellExecute(Handle, 'open', 'http://www.miktex.org/', nil, nil, SW_SHOW);
end;

end.

